package com.czy.owner.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.entity.GoodsEntity;
import com.czy.owner.entity.WorkShowEntity;
import com.czy.owner.utils.glide.GlideUtils;
import com.czy.owner.widget.RoundImageView;

import java.util.List;

/**
 * Created by King on 2017-05-23
 */

public class ConfirmOrderAdapter extends BaseRecyclerAdapter<GoodsEntity, ConfirmOrderAdapter.ExtendViewHolder> {
    private ExtendView headerView;
    private ExtendView footerView;
    private static final int HEADER_ITEM = 91;
    private static final int FOOTER_ITEM = 92;


    public ConfirmOrderAdapter(Context context, List data) {
        super(context, data);
    }

    public ConfirmOrderAdapter(Context context, List data, boolean slideEnabled, boolean reboundEnabled) {
        super(context, data, slideEnabled, reboundEnabled);
    }

    @Override
    public int getItemCount() {
        return super.getItemCount() + getHeaderCount() + getFooterCount();
    }

    private int getHeaderCount(){
        return headerView == null ? 0 : 1;
    }

    private int getFooterCount(){
        return footerView == null ? 0 : 1;
    }
    @Override
    public int getItemViewType(int position) {
        if (headerView != null && position == 0) {
            return HEADER_ITEM;
        }
        if (position - getHeaderCount() >= data.size()) {
            return FOOTER_ITEM;
        }
        return super.getItemViewType(position);
    }

    @Override
    public void onBindViewHolder(ExtendViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        switch (getItemViewType(position)){
            case HEADER_ITEM:
                headerView.onBindView(holder);
                return;
            case FOOTER_ITEM:
                footerView.onBindView(holder);
                return;
        }
        if(holder instanceof ConfirmOrderViewHolder){
            ConfirmOrderViewHolder viewHolder = (ConfirmOrderViewHolder) holder;
            if(TextUtils.isEmpty(data.get(position - getHeaderCount()).getGoodsLogo())){
                viewHolder.ivLogo.setImageResource(R.mipmap.default_icon);
            }else{
                GlideUtils.loadImage(mContext, data.get(position - getHeaderCount()).getGoodsLogo(), viewHolder.ivLogo, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                    @Override
                    public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {
                    }

                    @Override
                    public void onLoadingError(String source, Exception e) {
                    }
                }, R.mipmap.default_icon, R.mipmap.default_icon);
            }
            viewHolder.tvName.setText(data.get(position - getHeaderCount()).getGoodsName());
            viewHolder.tvPrice.setText("" +data.get(position - getHeaderCount()).getGoodsSpecValuesPrice());
            viewHolder.tvCounter.setText("X " + data.get(position - getHeaderCount()).getCount());
        }else{
            throw new NullPointerException("ConfirmOrderViewHolder not found!");
        }

    }

    @Override
    public ExtendViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case HEADER_ITEM:
                return new ExtendViewHolder(headerView.onCreateView(parent));
            case FOOTER_ITEM:
                return new ExtendViewHolder(footerView.onCreateView(parent));
        }
        return new ConfirmOrderViewHolder(layoutInflater.inflate(R.layout.item_confirm_order_recycler, parent, false));
    }


    public void addHeader(ExtendView itemView) {
        headerView = itemView;
        notifyItemInserted(0);
    }

    public void addFooter(ExtendView itemView) {
        footerView= itemView;
        notifyItemInserted(getItemCount() - 1);
    }

    public class ConfirmOrderViewHolder extends ExtendViewHolder {
        private RoundImageView ivLogo;
        private TextView tvName,tvPrice,tvCounter;

        public ConfirmOrderViewHolder(View itemView) {
            super(itemView);
            ivLogo = (RoundImageView) itemView.findViewById(R.id.iv_item_goods_logo);
            tvName = (TextView) itemView.findViewById(R.id.tv_item_goods_name);
            tvPrice = (TextView)itemView.findViewById(R.id.tv_item_goods_price);
            tvCounter = (TextView)itemView.findViewById(R.id.tv_item_goods_counter);
        }
    }


    public interface ExtendView {
        View onCreateView(ViewGroup parent);

        void onBindView(ViewHolder headerView);
    }

    public class ExtendViewHolder extends BaseRecyclerAdapter.ViewHolder{
        public ExtendViewHolder(View itemView) {
            super(itemView);
        }
    }
}

